﻿using System;
using System.Windows;

namespace Radovici.Ctrls.ContentControls
{
    public class Dropable : Dragable
    {
        public event Action<Dropable> DropStarted;
        public event Action<Dropable> DropStopped;

        public object Source
        {
            get { return SourceDataContext ?? DataContext; }
        }

        #region DropEnabled
        public bool DropEnabled
        {
            get { return (bool)GetValue(DropEnabledProperty); }
            set { SetValue(DropEnabledProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DropEnabled.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DropEnabledProperty = DependencyProperty.Register("DropEnabled", typeof(bool), typeof(Dropable), new PropertyMetadata(true, OnDropEnabledChanged));

        private static void OnDropEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
        #endregion

        #region SourceDataContext
        public object SourceDataContext
        {
            get { return (object)GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SourceDataContext.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TargetProperty = DependencyProperty.Register("SourceDataContext", typeof(object), typeof(Dropable), new PropertyMetadata(null));
        #endregion

        #region Protected methods
        protected virtual void OnDropStarted()
        {
            DropTarget.Monitor(this);
            if (DropStarted != null)
            {
                DropStarted(this);
            }            
        }

        protected virtual void OnDropStopped()
        {
            if (DropStopped != null)
            {
                DropStopped(this);
            }            
        }

        protected override void OnDragStarted()
        {
            base.OnDragStarted();
            OnDropStarted();
        }

        protected override void OnDragStopped()
        {
            base.OnDragStopped();
            OnDropStopped();
        }
        #endregion
    }
}
